<template>
  <div class="loginbBody">
        <div class="loginDiv">
            <div class="login-content">
                <h1 class="login-title"><b>用户登录</b></h1>
                
                <el-form :model="loginForm" label-width="100px"
                         :rules="rules" ref="loginForm">
                    <el-form-item label="名字" prop="name">
                        <el-input style="width: 200px" type="text" v-model="loginForm.name"
                                  autocomplete="off" size="small"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input style="width: 200px" type="password" v-model="loginForm.password"
                                  show-password autocomplete="off" size="small"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="confirm">确 定</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "login",
        data(){
            return{
                loginForm:{
                    name:'',
                    password:''
                },

                // 输入信息长度验证
                rules:{
                    name: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 5, message: '用户名长度在 2 到 5 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输密码', trigger: 'blur' },
                        { min: 2, max: 5, message: '密码长度在 2 到 5 个字符', trigger: 'blur' }
                    ]
                }

            }
        },
        methods:{
            // 登录后跳转到主页
            confirm(){
                this.$refs.loginForm.validate((valid) => {
                    if (valid) { //valid成功为true，失败为false
                        //去后台验证用户名密码，并返回token
                        this.$axios.post('/login',this.loginForm).then(res=>{
                            console.log(res.data)
                            if(res.data.state==1){
                                //存储token到本地
                                this.$store.commit("SET_TOKEN",res.data.vData.token);
                                //跳转到主页
                                this.$router.replace('/mylist');
                            }else{
                                alert('用户名或密码错误！');
                                return false;
                            }
                        });
                    } else {
                        console.log('校验失败');
                        return false;
                    }
                });
            }
        }
    }

</script>


<style scoped >
    .loginbBody {
        width: auto;
        height: 100%;
        background-color: #B3C0D1;
    }
    .loginDiv {
        position: absolute;
        top: 40%;
        left: 50%;
        margin-top: -200px;
        margin-left: -250px;
        width: 450px;
        height: 330px;
        background: #fff;
        border-radius: 5%;

    }
    .login-title {
        margin: 20px 0;
        text-align: center;
    }
    .login-content {
        width: 400px;
        height: 250px;
        position: absolute;
        top: 25px;
        left: 25px;
    }
</style>
